package com.example.courseapp

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

import android.widget.TextView
import com.example.courseapp.data.DataPreferences
import com.example.courseapp.data.LoginDataSource
import com.example.courseapp.ui.login.LoginActivity

class MainActivity : AppCompatActivity() {

    val dataPreferences= DataPreferences()
    // 初始化数据
    var dataSource = LoginDataSource()
    // 获取
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.activity_course)
        //初始化数据
        dataSource.initData()
        //检查登陆情况
        chekLogin()

        // 获取页面内容
        val title:TextView = findViewById(R.id.title_text)
        Log.d("data  main user ex?", dataPreferences.getUserInfo(this)!!.userName)
        title.text = dataPreferences.getUserInfo(this)!!.userName+"(退出登陆)"
        // 设置字体颜色
        title.setTextColor(Color.WHITE)
        Log.d("data main username", dataPreferences.getUserInfo(this)!!.userName)
        InitView()
        title.setOnClickListener(){
            excitlDialog(title.text.toString())
        }

    }

    //判断是否登录
    fun chekLogin(){
        // 判断用户名是否存在
        val isLogin:Boolean = try {
            Log.d("data checkLogin", dataPreferences.getUserInfo(this)!!.userName)
            dataPreferences.hasUserInfo(this)
        }catch (e:Exception){
            //设置一个临时数据
            dataPreferences.saveUserInfo(this,"tempName","123456")
            //Log.d("MainActivity",dataPreferences.takeName(this).toString())
            false
        }
        //Log.d("MainActivity",dataPreferences.takeName(this).toString())
        Log.d("MainActivity",isLogin.toString())
        if (!isLogin){
            //跳转到登录页面
            IntentLogin()
        }else
            dataPreferences.getUserInfo(this)
    }

    // 注销登录
    fun layout(){
        dataPreferences.clear(this)
        // 跳转到登录页面
        IntentLogin()
    }
    //跳转到登陆页面
    fun IntentLogin(){
        val intent=Intent(this,LoginActivity::class.java)
        startActivity(intent)
        // 结束activity
        finish()
    }

    fun InitView(){
        Log.d("data initView", dataPreferences.getUserInfo(this)!!.userName)
        val uName=dataPreferences.getUserInfo(this)!!.userName
        //获取用户名对应的列表
        Log.d("coursedata", dataSource.courseMap.toString())
        val res: Resources = resources
        //绑定周一的数据
        bindCourseInWeek(res,"Mon_")
        bindCourseInWeek(res,"Tue_")
        bindCourseInWeek(res,"Wed_")
        bindCourseInWeek(res,"Thu_")
        bindCourseInWeek(res,"Fri_")
        bindCourseInWeek(res,"Sat_")
        bindCourseInWeek(res,"Sun_")

    }

    // 绑定课程
    fun bindCourseInWeek(res:Resources, week:String){
        for(i in 1 .. 5 ) {
            // 遍历id
            val id = res.getIdentifier(week + i, "id", packageName)
            // Log.d("data bind week"+week+i,id.toString())
            val course:TextView = findViewById(id)
            when(week){
                "Mon_" ->{
                    course.text= dataSource.courseMap[1]?.get(i-1) ?: "无数据"
                    //Log.d("data course detial ", course.text.toString())
                    if(course.text!="") {
                        //设置背景颜色
                        course.setBackgroundColor(res.getColor(R.color.blueC))
                        course.setLinkTextColor(Color.BLACK)

                        course.setTextColor(Color.WHITE)
                        //绑定弹框
                        course.setOnClickListener {
                            detialDialog(course.text as String,i,week)
                        }

                    }else
                        course.setBackgroundColor(Color.WHITE)

                }
                "Tue_" -> {
                    course.text = dataSource.courseMap[2]?.get(i - 1) ?: "无数据"
                    if(course.text!="") {
                        //设置背景颜色
                        course.setBackgroundColor(res.getColor(R.color.blueB))
                        course.setTextColor(Color.WHITE)
                        //绑定弹框
                        course.setOnClickListener {
                            detialDialog(course.text as String,i,week)
                        }
                    }else
                        course.setBackgroundColor(Color.WHITE)
                }
                "Wed_" -> {
                    course.text = dataSource.courseMap[3]?.get(i - 1) ?: "无数据"
                    if(course.text!="") {
                        course.setBackgroundColor(res.getColor(R.color.greenB))
                        course.setTextColor(Color.WHITE)
                        //绑定弹框
                        course.setOnClickListener {
                            detialDialog(course.text as String,i,week)
                        }
                    }else
                        course.setBackgroundColor(Color.WHITE)

                }
                "Thu_" -> {
                    course.text = dataSource.courseMap[4]?.get(i - 1) ?: "无数据"
                    if(course.text!="") {
                        course.setBackgroundColor(res.getColor(R.color.greenC))
                        course.setTextColor(Color.WHITE)
                        //绑定弹框
                        course.setOnClickListener {
                            detialDialog(course.text as String,i,week)
                        }
                    }else
                        course.setBackgroundColor(Color.WHITE)
                }
                "Fri_" -> {
                    course.text = dataSource.courseMap[5]?.get(i - 1) ?: "无数据"
                    if(course.text!="") {
                        course.setBackgroundColor(res.getColor(R.color.brownC))
                        course.setTextColor(Color.WHITE)
                        //绑定弹框
                        course.setOnClickListener {
                            detialDialog(course.text as String,i,week)
                        }
                    }else
                        course.setBackgroundColor(Color.WHITE)
                }
                "Sat_" -> {
                    course.text = dataSource.courseMap[6]?.get(i - 1) ?: "无数据"
                    if(course.text!="") {
                        //设置背景颜色
                        course.setBackgroundColor(res.getColor(R.color.greenB))
                        course.setTextColor(Color.WHITE)
                        //绑定弹框
                        course.setOnClickListener {
                            detialDialog(course.text as String,i,week)
                        }
                    }else
                        course.setBackgroundColor(Color.WHITE)
                }
                "Sun_" -> {
                    course.text = dataSource.courseMap[7]?.get(i - 1) ?: "无数据"
                    if(course.text!="") {
                        course.setBackgroundColor(res.getColor(R.color.purpleC))
                        course.setTextColor(Color.WHITE)
                        //绑定弹框
                        course.setOnClickListener {
                            detialDialog(course.text as String,i,week)
                        }
                    }else
                        course.setBackgroundColor(Color.WHITE)
                }
            }

        }
    }

    //课程详情弹框
    fun detialDialog(courseTitle:String,key:Int,week:String){
        val items = arrayOf("班级：软件S4", "教师：张勇", "教室：H101","节次："+dataSource.courseTimeMap.get(key))
        AlertDialog.Builder(this).apply {
            setTitle(courseTitle).setItems(items, DialogInterface.OnClickListener { dialog, which ->
                    Log.d("data detial",week)
            })
            setCancelable(false)
            setPositiveButton("确定") { dialog, which ->
            }
            setNegativeButton("关闭") { dialog, which ->
            }
            show()
        }
    }

    // 退出弹框
    fun excitlDialog(username:String){
        AlertDialog.Builder(this).apply {
            setTitle("退出登录")
            setMessage("你确认退出吗？ "+username+"同学")
            setCancelable(false)
            setPositiveButton("确定") { dialog, which ->
                layout()
            }
            setNegativeButton("取消") { dialog, which ->
            }
            show()
        }
    }

    // 跳转详情
    fun IntentDetial(){
        val intent=Intent(this,DetailActivity::class.java)
        startActivity(intent)
        finish()
    }




    

}